package com.jt.common.net;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Tomcat {
    public static void main(String[] args) throws IOException {
       //1.构建一个Java中的服务端对象(ServerSocket),并在指定端口上进行监听(例如9999)
        ServerSocket server=new ServerSocket(9999);
        System.out.println("server start success");
       //2.等待客户端连接，并处理客户端请求
        while(true){
            //等待客户端(Socket)的连接(accept方法用于接收客户端请求)
            Socket client=server.accept();//阻塞式方法(没有连接请求，线程休眠，让出CPU)
            new Thread(){//1
                @Override
                public void run() {
                    System.out.println("client:"+client);
                    try {
                        //获取输出流对象，基于此对象向客户端响应hello client
                        OutputStream out = client.getOutputStream();
                        String data = "HTTP/1.1 200 OK \n\r" +
                                "Content-Type: text/html;charset=utf-8 \n\r" +
                                "\n\r" +
                                "hello client";
                        out.write(data.getBytes());
                        out.flush();
                    }catch (Exception e){}
                }
            }.start();
        }
    }
}
